<template>
  <div>
    <iframe
      id="test"
      src="https://m.amap.com/picker/?key=608d75903d29ad471362f8c58c550daf"
      width="100%"
      :height="heights"
      frameborder="0"
    ></iframe>
  </div>
</template>
<script
  type="text/javascript"
  src="https://webapi.amap.com/maps?v=1.3&key=d4332e5adb8b584442266763d20b978c"
></script>
<script>
export default {
  name: "GeographicalMap",
  data() {
    return {
      heights: 0
      // location: ""
    };
  },
  mounted() {
    let _this = this;
    // _this.getLocation();
    let dsBridge = require("dsbridge");
    dsBridge.call("goBack", "", function() {
      history.go(-1);
    });

    sessionStorage.removeItem("Hsite");
    sessionStorage.removeItem("Hjd");
    sessionStorage.removeItem("Hwd");
    let center = "";
    if (sessionStorage.getItem("jd") && sessionStorage.getItem("wd")) {
      let jd = sessionStorage.getItem("jd");
      let wd = sessionStorage.getItem("wd");
      center = jd + "," + wd;
      console.log("经纬度:" + jd + "," + wd);
    } else {
      center = "114.502205,38.045909";
    }
    let iframeUrl =
      "https://m.amap.com/picker/?key=608d75903d29ad471362f8c58c550daf&center=" +
      center;

    document.getElementById("test").src = iframeUrl;

    _this.heights = window.innerHeight + "px";
    //   // console.log(this.height)
    var iframe = document.getElementById("test").contentWindow;
    document.getElementById("test").onload = function() {
      iframe.postMessage("hello", "https://m.amap.com/picker/");
    };
    window.addEventListener("message", this.coordinates);
  },
  beforeDestroy() {
    window.removeEventListener("message", this.coordinates);
  },

  methods: {
    coordinates(e) {
      var address = e.data.address;
      // console.log(e.data.location)
      this.$emit("functionName", address, e.data.location);
      // console.log(address)
      let cordinate = e.data.location.split(",");
      // console.log(cordinate)
      sessionStorage.setItem("site", address);
      sessionStorage.setItem("jd", cordinate[0]);
      sessionStorage.setItem("wd", cordinate[0]);
      this.$router.go(-1);
    }
  }
};
</script>
<style></style>
